package com.liuzh.fitnessplan.activity

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.util.Log
import com.liuzh.fitnessplan.R
import com.liuzh.fitnessplan.base.BaseActivity
import com.liuzh.fitnessplan.database.bean.Sports
import com.liuzh.fitnessplan.tools.RoomDatabaseUtil
import java.util.*

class FlashActivity : BaseActivity() {
    private lateinit var timerTask : TimerTask
    private val timer = Timer()
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_flash)

        val sp = getSharedPreferences("appInfo", Context.MODE_PRIVATE)
        if(sp.getBoolean("isLogin",false)){
            timerTask= object : TimerTask(){
                override fun run() {
                    toMain(this@FlashActivity)
                    Log.d("FlashActivity","跳转Main  UserId = ${sp.getInt("userId",0)}")
                    finish()
                }
            }
        }else{
            timerTask= object : TimerTask(){
                override fun run() {
                    toLogin(this@FlashActivity)
                    Log.d("FlashActivity","跳转Login")
                    finish()
                }
            }
        }

        timer.schedule(timerTask,1500)
        Thread{
            initData()
        }.start()
    }

    fun toLogin(context : Context){startActivity(Intent(context,LoginActivity::class.java))}

    fun toMain(context : Context){startActivity(Intent(context,MainActivity::class.java))}


    private fun initData() {
        val db = RoomDatabaseUtil.instance.getDatabase(this)
        if(db.sportsDao().getData().isNotEmpty()){
            return
        }
        val sports1 = Sports("慢跑","适中","30分钟")
        val sports2 = Sports("快跑","适中","20分钟")
        val sports3 = Sports("长跑","较强","60分钟")
        val sports4 = Sports("快走","较弱","70分钟")
        val sports5 = Sports("引体向上","适中","10分钟")
        val sports6 = Sports("波比跳","较强","15分钟")
        val sports7 = Sports("俯卧撑","适中","10分钟")

        db.sportsDao().addData(sports1,sports2,sports3,sports4,sports5,sports6,sports7)
        Log.d("FlashActivity","数据加载成功")
        db.close()
    }
}
